package com.fjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjh.domain.UserCollection;
import com.fjh.es.ProdEs;
import com.fjh.feign.UserCollectProdFeign;
import com.fjh.mapper.UserCollectionMapper;
import com.fjh.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private UserCollectProdFeign userCollectProdFeign;

    /**
     * 查询用户的收藏数量
     * @param userId
     * @return
     */
    @Override
    public Integer getUserCollectCount(String userId) {
        return userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId,userId)
        );
    }

    /**
     * 分页查询用户的收藏商品信息
     * 1.还是要查询用户的收藏表
     * 根据用户的id分页查询收藏信息
     * 拿到商品的id集合
     * 远程调用搜索模块  拿到prodEs集合
     * 再组装
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<ProdEs> getUserCollectPage(Page<UserCollection> page, String userId) {
        Page<ProdEs> prodEsPage = new Page<>(page.getCurrent(), page.getSize());
        //查询用户收藏
        Page<UserCollection> collectionPage = userCollectionMapper.selectPage(page, new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .orderByDesc(UserCollection::getCreateTime)
        );

        List<UserCollection> collections = collectionPage.getRecords();
        if (CollectionUtils.isEmpty(collections)){
            prodEsPage.setTotal(0L);
            prodEsPage.setRecords(Collections.emptyList());
            return prodEsPage;
        }
        //拿到商品的ids
        List<Long> prodIds = collections.stream()
                .map(UserCollection::getProdId)
                .collect(Collectors.toList());
        //远程调用搜索模块  获取商品信息集合
        List<ProdEs> prodEsList = userCollectProdFeign.getProdEsByIds(prodIds);
        if (!CollectionUtils.isEmpty(prodEsList)){
            prodEsPage.setRecords(prodEsList);
            prodEsPage.setTotal(collectionPage.getTotal());
        }
        return prodEsPage;
    }

    /**
     * 查询用户是否收藏该商品
     * @param userId
     * @param prodId
     * @return
     */
    @Override
    public Boolean isCollection(String userId, Long prodId) {
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getProdId, prodId)
        );
        return count>0;
    }

    /**
     * 添加或者取消收藏该商品
     * @param userId
     * @param prodId
     */
    @Override
    public void addOrCancel(String userId, Long prodId) {
        //先根据用户id和商品id查询到一条记录
        UserCollection oldCollect = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getProdId, prodId)
        );
        if (ObjectUtils.isEmpty(oldCollect)){
            //添加收藏
            UserCollection userCollection = new UserCollection();
            userCollection.setProdId(prodId);
            userCollection.setCreateTime(new Date());
            userCollection.setUserId(userId);
            userCollectionMapper.insert(userCollection);
            return;
        }
        //取消收藏
        userCollectionMapper.deleteById(oldCollect.getId());
    }
}


